package com.hupu.jrs.components.notifier

import android.content.Context
import android.support.v4.app.NotificationCompat
import com.hupu.jrs.R
import com.hupu.jrs.db.Forum
import com.hupu.jrs.injector.PerService
import com.hupu.jrs.util.FormatUtil
import javax.inject.Inject

/**
 * Created by Stark.Tony on 2018/3/18.
 */
@PerService
class OfflineNotifier @Inject
constructor(private val mContext: Context) : Notifier() {

    fun notifyThreads(forum: Forum, offlineLength: Long) {
        val title = String.format("正在离线板块[%s]", forum.name)
        val content = String.format("节省流量%s", FormatUtil.formatFileSize(offlineLength))

        val builder = NotificationCompat.Builder(mContext)

        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentText(content)

        notify(Notifier.Companion.OfflineThreads, 0, builder)
    }

    fun notifyThreadsSuccess(forumSize: Int, threadsSize: Int, threadsLength: Long) {
        val title = String.format("%d个板块完成", forumSize)
        val content = String.format("共%d篇帖子，节省流量%s", threadsSize, FormatUtil.formatFileSize(threadsLength))

        val builder = NotificationCompat.Builder(mContext)

        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentText(content)

        notify(Notifier.Companion.OfflineThreads, 0, builder)
    }

    fun notifyPictureSuccess(picSize: Int, picLength: Long) {
        val title = "图片离线完成"
        val content = String.format("%s张图片，节省流量%s", picSize.toString(),
                FormatUtil.formatFileSize(picLength))

        val builder = NotificationCompat.Builder(mContext)

        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentText(content)

        this@OfflineNotifier.notify(Notifier.Companion.OfflinePicture, 0, builder)
    }

    fun notifyReplies(thread: Thread, replyLength: Long) {
        val title = String.format("正在离线[%s]的回复", thread)
        val content = String.format("节省流量%s", FormatUtil.formatFileSize(replyLength))

        val builder = NotificationCompat.Builder(mContext)

        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentText(content)

        notify(Notifier.Companion.OfflineReplies, 0, builder)
    }

    fun notifyRepliesSuccess(threadSize: Int, replySize: Int, replyLength: Long) {
        val title = String.format("%d篇帖子完成", threadSize)
        val content = String.format("共%d篇回复，节省流量%s", replySize, FormatUtil.formatFileSize(replyLength))

        val builder = NotificationCompat.Builder(mContext)

        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentText(content)

        notify(Notifier.Companion.OfflineReplies, 0, builder)
    }

    fun notify(request: Int, status: Int, builder: NotificationCompat.Builder) {
        val notification = builder.build()
        notify(request, notification)
    }
}
